package com.abcdefg.handlerdemo.demo3;

import lombok.SneakyThrows;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
    @Override
    @SneakyThrows    //简化异常处理，通过注解将try-catch简化，但还是会正常抛出异常
    public Object beforeBodyWrite(Object body,
                                  MethodParameter returnType,
                                  MediaType selectedContentType,
                                  Class selectedConverterType,
                                  ServerHttpRequest request,
                                  ServerHttpResponse response) {
        return null;
    }

    //如果supports方法返回true,则调用beforeBodyWrite方法
    //如果返回false，则不会调用beforeBodyWrite方法
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return false;
    }
}
